/**
 * Created by zhanglihe on 15/9/9.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function( $, common, ready, template, Collection, Model, dialog ) {

    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.event();
            this.fetch();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'admins',
                condition: {limit: 10, offset: 0},
                model: {
                    tag_name: 'tr',
                    url: _global.url.api + 'admin/{id}',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-trash:click': 'trash',
                        '.item-enable': 'enable',
                        '.item-disable': 'disable'
                    },
                    edit: function(e) {
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            title: '编辑账号：' + _this.data.real_name,
                            width: 400,
                            content: template('edit-tpl', _this.data),
                            ok: function() {
                                _this.data.real_name = $('#real_name').val();
                                if($('#password').val()) {
                                    _this.data.password = $('#password').val();
                                }
                                if(!_this.put()) {
                                    console.log('put false');
                                    return false;
                                }
                            }
                        }).show();
                    },
                    trash: function(e) {
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            content: '确定要删除吗?',
                            ok: function() {
                                _this.del();
                            },cancel: function() {}
                        }).show(e.target);
                    },
                    enable: function(e) {
                        e.preventDefault();
                        if(new Model({
                            url: _global.url.api + 'admin_enable/' + this.get_data('id')
                        }).put()) {
                            this.fetch();
                        }
                    },
                    disable: function(e) {
                        e.preventDefault();
                        if(new Model({
                                url: _global.url.api + 'admin_disable/' + this.get_data('id')
                            }).put()) {
                            this.fetch();
                        }
                    },
                    valid_config: {
                        username: 'required',
                        password: 'required',
                        real_name: 'required'
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            var _this = this;
            $('#btn-add').on('click',function(e) {
                e.preventDefault();
                dialog({
                    title: '添加账号',
                    width: 400,
                    content: template('edit-tpl'),
                    ok: function() {
                        data = {
                            username: $('#username').val(),
                            password: $('#password').val(),
                            real_name: $('#real_name').val()
                        };
                        if(!new Model({
                                url: _global.url.api + 'admin',
                                data:data,
                                valid_config: {
                                    username: 'required',
                                    password: 'required',
                                    real_name: 'required'
                                }
                            }).post()) {
                            return false;
                        }
                        _this.fetch();
                    },cancel: function() {}
                }).show(e.target);
            });
            //启用
            $('#btn-enable').on('click',function(e){
                e.preventDefault();
                _this.collection.condition.status = $('#btn-enable').attr('status');
                _this.collection.reload();
            });
            //禁用
            $('#btn-disable').on('click',function(e){
                e.preventDefault();
                _this.collection.condition.status = $('#btn-disable').attr('status');
                _this.collection.reload();
            });
            //全部
            $('#btn-all').on('click',function(e){
                e.preventDefault();
                _this.collection.condition.status = $('#btn-all').attr('status');
                _this.collection.reload();
            });
            //搜索
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });
        }
    };

    new index();

});